﻿using System;
using System.Collections.Generic;
using NHibernate;
using Repositories.UoW;

namespace Repositories.Repository
{
    public class Repository<T,ID>:IRepository<T,ID>
    {
        protected ISession Session
        {
            get { return SessionFactory.GetSession(); }
        }

        private readonly Type _type = typeof (T);

        #region Implementation of IRepository<T,ID>

        public T Get(ID id)
        {
            try
            {
                return Session.Load<T>(id);
            }
            catch (HibernateException e)
            {
                throw e;
            }
        }

        public IList<T> GetList()
        {
            try
            {
                return Session.CreateCriteria(_type).List<T>();
            }
            catch (HibernateException e)
            {

                throw e;
            }
        }

        public int Insert(T value)
        {
            try
            {
                Session.BeginTransaction();
                Session.Save(value);
                Session.Transaction.Commit();
                //Session.Flush();
                return 1;
            }
            catch (HibernateException e)
            {
                Session.Transaction.Rollback();
                return 0;
            }

        }

        public int Update(T value)
        {
            try
            {
                Session.BeginTransaction();
                Session.Update(value);
                Session.Transaction.Commit();
                //Session.Flush();
                return 1;
            }
            catch (HibernateException e)
            {
                Session.Transaction.Rollback();
                return 0;
            }
        }

        public int Delete(T value)
        {
            try
            {
                Session.BeginTransaction();
                Session.Delete(value);
                Session.Transaction.Commit();
                //Session.Flush();
                return 1;
            }
            catch (HibernateException e)
            {
                Session.Transaction.Rollback();
                return 0;
            }
        }

        public int InsertList(IList<T> list)
        {
            try
            {
                Session.BeginTransaction();
                foreach (T item in list)
                {
                    Session.Save(item);
                }
                Session.Transaction.Commit();
                //Session.Flush();
                return list.Count;
            }
            catch (HibernateException e)
            {
                Session.Transaction.Rollback();
                return 0;
            }
        }

        public int UpdateList(IList<T> list)
        {
            try
            {
                Session.BeginTransaction();
                foreach (T item in list)
                {
                    Session.Update(item);
                }
                Session.Transaction.Commit();
                //Session.Flush();
                return list.Count;
            }
            catch (HibernateException e)
            {
                Session.Transaction.Rollback();
                return 0;
            }
        }

        public int DeleteList(IList<T> list)
        {
            try
            {
                Session.BeginTransaction();
                foreach (T item in list)
                {
                    Session.Delete(item);
                }
                Session.Transaction.Commit();
                //Session.Flush();
                return list.Count;
            }
            catch (HibernateException e)
            {
                Session.Transaction.Rollback();
                return 0;
            }
        }

        #endregion
    }
}